package com.xuecheng.media.service.impl;

import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaProcessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class MediaProcessServiceImpl implements MediaProcessService {

    private final MediaFilesMapper mediaFilesMapper;


    private final MediaProcessMapper mediaProcessMapper;

    private final MediaProcessHistoryMapper mediaProcessHistoryMapper;


    /**
     * 查询待处理的数据
     * @param shardIndex 分片序号
     * @param shardTotal 分片总数
     * @param count 获取记录数
     * @return
     */
    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
        return mediaProcesses;
    }


    /**
     * 加锁
     * @param id
     * @return
     */
    @Override
    public int startTask(Long id) {
        return mediaProcessMapper.startTask(id);
    }


    /**
     *
     * @param taskId 任务id
     * @param status 任务状态
     * @param fileId 文件id
     * @param url
     * @param errorMsg 错误信息
     */
    //任务处理完成需要更新任务处理结果，任务执行成功更新视频的URL、及任务处理结果，将待处理任务记录删除，同时向历史任务表添加记录。
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        //查任务表，如果不存在直接返回
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if(mediaProcess==null){
            return ;
        }
        //处理失败，更新结果继续处理
        if(status.equals("3")){
            mediaProcess.setStatus("3");
            mediaProcess.setFailCount(mediaProcess.getFailCount()+1);
            mediaProcess.setErrormsg(errorMsg);
            mediaProcessMapper.updateById(mediaProcess);
            log.info("更新任务失败，任务信息：{}",mediaProcess);
            return;
        }
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);

        //文件标识
        mediaProcessHistory.setFileId(mediaFiles.getFileId());
        //文件名称
        mediaProcessHistory.setFilename(mediaProcessHistory.getFilename());
        //存储源
        mediaProcessHistory.setBucket(mediaProcessHistory.getBucket());
        //文件路径
        mediaProcessHistory.setFilePath(mediaProcessHistory.getFilePath());
        //文件状态
        mediaProcessHistory.setStatus(status);
        //上传时间
        mediaProcessHistory.setCreateDate(mediaFiles.getCreateDate());
        //完成时间
        mediaProcessHistory.setFinishDate(LocalDateTime.now());
        //访问地址
        mediaProcessHistory.setUrl(url);
        if(url!=null){
            //更新媒资文件中的url
            mediaFiles.setUrl(url);
            mediaFilesMapper.updateById(mediaFiles);
        }
        //失败次数
        mediaProcessHistory.setFailCount(mediaProcessHistory.getFailCount());
        //失败原因
        mediaProcessHistory.setErrormsg(errorMsg);
        //删除过程表记录
        mediaProcessMapper.deleteById(taskId);
        //向历史表中添加数据
        mediaProcessHistoryMapper.insert(mediaProcessHistory);
    }



}
